package com.cmc.cloud.cmclink.doc.service.impl;

import javax.annotation.Resource;

import com.cmes.framework.ex.Assertion;import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.cmc.cloud.cmclink.doc.constants.PatternSpecialCargoEmailConstants;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.PartnerSpecialCargoEmailErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.PartnerSpecialCargoEmailConvert;
import com.cmc.cloud.cmclink.doc.entity.PartnerSpecialCargoEmailDO;
import com.cmc.cloud.cmclink.doc.mapper.PartnerSpecialCargoEmailMapper;
import com.cmc.cloud.cmclink.doc.service.PartnerSpecialCargoEmailService;
import com.cmc.cloud.cmclink.doc.util.sepcial.CommonUtil;
import com.cmc.cloud.cmclink.doc.vo.special.partneremailvo.PartnerSpecialCargoEmailBaseVO;
import com.cmc.cloud.cmclink.doc.vo.special.partneremailvo.PartnerSpecialCargoEmailCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.special.partneremailvo.PartnerSpecialCargoEmailPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.special.partneremailvo.PartnerSpecialCargoEmailUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;

/**
 * 合作方 Email 维护 Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class PartnerSpecialCargoEmailServiceImpl implements PartnerSpecialCargoEmailService {

    @Resource
    private PartnerSpecialCargoEmailMapper partnerSpecialCargoEmailMapper;

    @Override
    public void createPartnerSpecialCargoEmail(PartnerSpecialCargoEmailCreateReqVO createReqVO) {
        // 检验邮箱有效性
        validEmail(createReqVO.getDgEmails(), PatternSpecialCargoEmailConstants.DG_EMAIL_NAME);
        validEmail(createReqVO.getOdoEmails(), PatternSpecialCargoEmailConstants.ODO_EMAIL_NAME);
        validEmail(createReqVO.getFlexiEmails(), PatternSpecialCargoEmailConstants.FLEXI_EMAIL_NAME);
        validEmail(createReqVO.getOogEmails(), PatternSpecialCargoEmailConstants.OOG_EMAIL_NAME);

        // 校验存在
        validateUnique(createReqVO, null);

        // 插入
        PartnerSpecialCargoEmailDO partnerSpecialCargoEmail =
            PartnerSpecialCargoEmailConvert.INSTANCE.convert(createReqVO);
        partnerSpecialCargoEmailMapper.insert(partnerSpecialCargoEmail);
    }

    private void validateUnique(PartnerSpecialCargoEmailBaseVO vo, Long id) {
        LambdaQueryWrapperX<PartnerSpecialCargoEmailDO> queryWrapperX =
            new LambdaQueryWrapperX<PartnerSpecialCargoEmailDO>()
                .eqIfPresent(PartnerSpecialCargoEmailDO::getPartnerCode, vo.getPartnerCode())
                .eqIfPresent(PartnerSpecialCargoEmailDO::getPol, vo.getPol())
                .eqIfPresent(PartnerSpecialCargoEmailDO::getLaneCodes, vo.getLaneCodes());
        if (id != null) {
            queryWrapperX.ne(PartnerSpecialCargoEmailDO::getId, id);
        }
        if (partnerSpecialCargoEmailMapper.selectCount(queryWrapperX) != 0L) {
            throw ServiceExceptionUtil.exception(PartnerSpecialCargoEmailErrorCodeConstants.PARTNER_EMAIL_EXISTED);
        }
    }

    private void validEmail(String emailMix, String name) {
        String[] emails = emailMix.split(PatternSpecialCargoEmailConstants.EMAIL_SPLIT_SEMICOLON);
        for (String email : emails) {
            if (!CommonUtil.validEmail(email)) {
                Assertion.assertTrue(false).raise0(PartnerSpecialCargoEmailErrorCodeConstants.EMAIL_ILLEGAL.getCode(), name);
            }
        }
    }

    @Override
    public void updatePartnerSpecialCargoEmail(PartnerSpecialCargoEmailUpdateReqVO updateReqVO) {
        // 检验邮箱有效性
        validEmail(updateReqVO.getDgEmails(), PatternSpecialCargoEmailConstants.DG_EMAIL_NAME);
        validEmail(updateReqVO.getOdoEmails(), PatternSpecialCargoEmailConstants.ODO_EMAIL_NAME);
        validEmail(updateReqVO.getFlexiEmails(), PatternSpecialCargoEmailConstants.FLEXI_EMAIL_NAME);
        validEmail(updateReqVO.getOogEmails(), PatternSpecialCargoEmailConstants.OOG_EMAIL_NAME);

        // 校验存在
        validatePartnerSpecialCargoEmailExists(updateReqVO.getId());

        // 检验唯一性
        validateUnique(updateReqVO, updateReqVO.getId());

        // 更新
        PartnerSpecialCargoEmailDO updateObj = PartnerSpecialCargoEmailConvert.INSTANCE.convert(updateReqVO);
        partnerSpecialCargoEmailMapper.updateById(updateObj);
    }

    @Override
    public void deletePartnerSpecialCargoEmail(Long id) {
        // 校验存在
        validatePartnerSpecialCargoEmailExists(id);
        // 删除
        partnerSpecialCargoEmailMapper.deleteById(id);
    }

    private void validatePartnerSpecialCargoEmailExists(Long id) {
        if (partnerSpecialCargoEmailMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(PartnerSpecialCargoEmailErrorCodeConstants.PARTNER_EMAIL_NOT_EXISTED);
        }
    }

    @Override
    public PartnerSpecialCargoEmailDO getPartnerSpecialCargoEmail(Long id) {
        return partnerSpecialCargoEmailMapper.selectById(id);
    }

    @Override
    public PageResult<PartnerSpecialCargoEmailDO>
        getPartnerSpecialCargoEmailPage(PartnerSpecialCargoEmailPageReqVO reqVO) {
        LambdaQueryWrapperX<PartnerSpecialCargoEmailDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eqIfPresent(PartnerSpecialCargoEmailDO::getPartnerCode, reqVO.getPartnerCode())
            .eqIfPresent(PartnerSpecialCargoEmailDO::getPol, reqVO.getPol())
            .eqIfPresent(PartnerSpecialCargoEmailDO::getLaneCodes, reqVO.getLaneCodes())
            .eqIfPresent(PartnerSpecialCargoEmailDO::getPartnerNameEn, reqVO.getPartnerNameEn())
            .eqIfPresent(PartnerSpecialCargoEmailDO::getPartnerNameCn, reqVO.getPartnerNameCn());
        return partnerSpecialCargoEmailMapper.selectPage(reqVO, queryWrapperX);
    }
}
